package com.sgb.kjwl.view.widget.transport;

import android.content.Context;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppBiddingSourceListPopLayoutBinding;
import com.sgb.kjwl.model.entity.transport.FreightProEntity;
import com.sgb.kjwl.utils.DisplayUtil;
import com.sgb.kjwl.view.ui.adapter.map.AdapterOnItemClick;
import com.sgb.kjwl.view.ui.adapter.map.BasePopWindow;
import com.sgb.kjwl.view.ui.adapter.map.PopViewHolder;
import com.sgb.kjwl.view.ui.adapter.transport.SourcePopListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe:    货源项目列表弹窗（报价单位地图页面）
 * Author:  yangLei
 * Date:    2021/8/30
 */
public class SourceListPopWindow extends BasePopWindow {

    private AppBiddingSourceListPopLayoutBinding mBinding;
    private SourcePopListAdapter listAdapter;
    private List<FreightProEntity> data = new ArrayList<>();

    private ItemClickResult itemClickResult;

    public SourceListPopWindow(Context context) {
        super(context);
    }

    public ItemClickResult getItemClickResult() {
        return itemClickResult;
    }

    public void setItemClickResult(ItemClickResult itemClickResult) {
        this.itemClickResult = itemClickResult;
    }

    public List<FreightProEntity> getData() {
        return data;
    }

    public void setData(List<FreightProEntity> data) {
        this.data = data;
        if (data != null && data.size() > 0) {
            if (listAdapter != null)
                listAdapter.setDatas(data);
            if (data.size() >= 7) {
                //设置固定高度
                setHeight(DisplayUtil.convertDpToPixel(mContext, 320));
            } else {
                //设置为内容自适应
                setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        }
    }

    @Override
    protected int popLayout() {
        return R.layout.app_bidding_source_list_pop_layout;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (AppBiddingSourceListPopLayoutBinding) holder.getmBinding();
        //地图页列表初始化
        if (listAdapter == null)
            listAdapter = new SourcePopListAdapter(mContext, null);
        DividerItemDecoration divider2 = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        divider2.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_list_divider_5dp));
        mBinding.list.addItemDecoration(divider2);
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(mContext);
        mBinding.list.setLayoutManager(layoutManager2);
        mBinding.list.setAdapter(listAdapter);
        mBinding.list.setLoadingMoreEnabled(false);
        mBinding.list.setPullRefreshEnabled(false);

        listAdapter.setOnItemClick(new AdapterOnItemClick<FreightProEntity>() {
            @Override
            public void onItemClick(FreightProEntity typeChooseEntity, int position) {
                if (listAdapter.getDatas().size() >= position + 1) {
                    for (int i = 0; i < listAdapter.getDatas().size(); i++) {
                        if (i == position) {
                            listAdapter.getDatas().get(i).setSelect(true);
                        } else {
                            listAdapter.getDatas().get(i).setSelect(false);
                        }
                    }
                    listAdapter.notifyDataSetChanged();
                }

                if (itemClickResult != null) {
                    itemClickResult.onResult(typeChooseEntity);
                    dismiss();
                }
            }
        });
    }

    public interface ItemClickResult {
        void onResult(FreightProEntity entity);
    }
}
